package cn.scut.post.service.impl;

import cn.scut.post.dao.PostInfoDao;
import cn.scut.post.pojo.Post;
import cn.scut.post.service.PostService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.logging.log4j.util.Strings;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class PostInfoService extends ServiceImpl<PostInfoDao, Post> implements PostService {
    PostInfoDao postInfoDao;

    public Post uploadPost(Post post) {
        postInfoDao.uploadPost(post);
        return post;
    }

    public List<Post> downloadPost(String openid) {
        LambdaQueryWrapper<Post> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.like(Strings.isNotEmpty(openid), Post::getOpenid, openid);
        List<Post> posts = postInfoDao.selectList(lambdaQueryWrapper);
        if (posts.size() == 0) {
            return null;
        }
        return posts;
    }

    public int deletePost(String openid) {
        return postInfoDao.deleteByPostId(openid);
    }

    @Autowired
    public void setPostInfoDao(PostInfoDao postInfoDao) {
        this.postInfoDao = postInfoDao;
    }
}
